<?php
/**
 * Nweb Editor
 *
 * This script is protected by copyright. Its use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.editor.model
 * @subpackage  row
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        https://nweb-editor.googlecode.com
 * @link        http://editor.nweb.pl
 */

namespace pl\nweb\editor\app\model\gateway;

/**
 * Model wiersza tabeli
 *
 * @category    source
 * @package     pl.nweb.editor.model
 * @subpackage  row
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
abstract class Row extends \Zend_Db_Table_Row
{
    /**
     * Zwraca wartość pola
     *
     * @param  string $field pole
     * @param  mixed $default domyślna wartość
     * @return string|null
     */
    public function get ($field, $default = null)
    {
        if(isset($this->{$field})) {
            return $this->{$field};
        }
        return $default;
    }

    /**
     * Formatuje datę do odpowiedniego formatu
     *
     * @param  string $field pole
     * @param  string $date data
     * @return void
     */
    protected function _setDate ($field, $date)
    {
        $time = strtotime($date);
        if($time > 1) {
            $this->{$field} = date('Y-m-d H:i:s', $time);
        }
        $this->{$field} = new \Zend_Db_Expr('NULL');
    }

    /**
     * Zwraca datę w zdefiniowanym formacie lub null
     *
     * @param  string $field pole
     * @param  string $format format
     * @return null|string
     */
    protected function _getDate ($field, $format = 'Y/m/d H:i:s')
    {
        if($date = $this->get($field)) {
            $time = strtotime($date);
            if($time > 1) {
                return date($format, $time);
            }
        }
        return null;
    }
}